using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using XFGameFramework;

public class GameSavePanel : Panel
{
    #region 字段
    [SerializeField]
    private Transform[] SaveButtons;
    private GameSaveController gameSaveController => Module.LoadController<GameSaveController>();
    private GameController gameController => Module.LoadController<GameController>();
    private TipPanel tipPanel;
    private StartPanel startPanel;
    #endregion
    private void RegisterSaveButtons()
    {
        for (int i = 0; i < SaveButtons.Length; i++)
        {
            int index = i;
            if (SaveButtons[index] == null) continue;

            Button button_Delete = SaveButtons[index].transform.Find("Button_SavedGame/GameNotSaveEmpty/Button_DeleteGame").GetComponent<Button>();
            Button button_CoveredGame = SaveButtons[index].transform.Find("Button_SavedGame/GameNotSaveEmpty/Button_CoveredGame").GetComponent<Button>();
            Button button_LoadingGame = SaveButtons[index].transform.Find("Button_SavedGame/GameNotSaveEmpty/Button_LoadingGame").GetComponent<Button>();
            Button Button_Game = SaveButtons[index].transform.Find("Button_SavedGame").GetComponent<Button>();
            Button button_emptyLoadingGame = SaveButtons[index].transform.Find("Button_SavedGame/GameSaveEmpty/Button_LoadingGame").GetComponent<Button>();
            Button button_emptyCoveredGame = SaveButtons[index].transform.Find("Button_SavedGame/GameSaveEmpty/Button_CoveredGame").GetComponent<Button>();

            button_Delete.onClick.RemoveAllListeners();
            button_Delete.onClick.AddListener(() => { OnBtnDeleteGameClick(index+1); });

            button_CoveredGame.onClick.AddListener(() => { OnBtnCoveredGameClick(index+1); });
            button_LoadingGame.onClick.AddListener(() => { OnBtnLoadingGameClick(index+1); });
            Button_Game.onClick.AddListener(() => { OnBtnGameClick(index+1); });
            button_emptyLoadingGame.onClick.AddListener(() => { OnBtnGameClick(index+1); });
            button_emptyCoveredGame.onClick.AddListener(() => { OnBtnCoveredGameClick(index+1); });
        }
        
    }

    private void UpdateSaveGame()
    {
        
        for (int i = 0; i< SaveButtons.Length; i++)
        {
            int index = i;
            if (SaveButtons[index] == null) continue;
            GameObject emptyobj = SaveButtons[index].transform.Find("Button_SavedGame/GameSaveEmpty").gameObject;
            GameObject notemptyobj = SaveButtons[index].transform.Find("Button_SavedGame/GameNotSaveEmpty").gameObject;

            Button button_Delete = SaveButtons[index].transform.Find("Button_SavedGame/GameNotSaveEmpty/Button_DeleteGame").GetComponent<Button>();
            Button button_LoadingGame = SaveButtons[index].transform.Find("Button_SavedGame/GameNotSaveEmpty/Button_LoadingGame").GetComponent<Button>();
            Button button_CoveredGame = SaveButtons[index].transform.Find("Button_SavedGame/GameNotSaveEmpty/Button_CoveredGame").GetComponent<Button>();

            Button button_emptyLoadingGame = SaveButtons[index].transform.Find("Button_SavedGame/GameSaveEmpty/Button_LoadingGame").GetComponent<Button>();
            Button button_emptyCoveredGame = SaveButtons[index].transform.Find("Button_SavedGame/GameSaveEmpty/Button_CoveredGame").GetComponent<Button>();

            Text text_days = SaveButtons[index].transform.Find("Button_SavedGame/GameNotSaveEmpty/GameSaveContent").GetComponent<Text>();
            Text text_date = SaveButtons[index].transform.Find("Button_SavedGame/GameNotSaveEmpty/GameSaveDate").GetComponent<Text>();

            // GameSaveModel gameSaveModel =gameSaveController.Get(index);
            TimeModel timeModel = gameSaveController.GetTimeModel(index+1);
            PlayerModel playerModel = gameSaveController.GetPlayerModel(index+1);
            GameState state = gameController.GetGameModel().gameState;
            if (state == GameState.Start)
            {
                button_Delete.gameObject.SetActive(true);
                button_LoadingGame.gameObject.SetActive(true);
                button_CoveredGame.gameObject.SetActive(false);
                button_emptyCoveredGame.gameObject.SetActive(false);
                button_emptyLoadingGame.gameObject.SetActive(true);
            }

            else
            {
                button_Delete.gameObject.SetActive(true);
                button_LoadingGame.gameObject.SetActive(false);
                button_CoveredGame.gameObject.SetActive(true);
                button_emptyCoveredGame.gameObject.SetActive(true);
                button_emptyLoadingGame.gameObject.SetActive(false);
            }

            emptyobj.SetActive(timeModel == null || playerModel == null);
            notemptyobj.SetActive(timeModel != null && playerModel != null);
            if (timeModel != null && playerModel != null)
            {
                TimeModel timeModel1 = gameSaveController.GetTimeModel(index+1);
                // print(gameSaveModel);
                // print(timeModel);
                // print(timeModel.days);
                text_days.text = string.Format("天数: {0}", timeModel1.days);
            }
        }
    }
    #region UI监听事件
    public void OnCloseClick()
    {
        Close();
    }
    public void OnBtnGameClick(int index)
    {
        // GameSaveModel gameSaveModel = gameSaveController.Get(index);
        // gameController.SetArchiveIndex(gameSaveModel.Id);
        // gameController.SetGameState(GameState.Game_Ready);

        // GameSaveModel gameSaveModel = gameSaveController.Get(index);
        TimeModel timeModel = gameSaveController.GetTimeModel(index);
        PlayerModel playerModel = gameSaveController.GetPlayerModel(index);

        GameState state = gameController.GetGameModel().gameState;
        if (state == GameState.Start)
        {
            if (timeModel != null && playerModel != null)
            {
                gameController.SetArchiveIndex(index);
                // gameSaveController.LoadGameSave(index);


                gameController.SetGameState(GameState.Game_Main);
            }
            else if (timeModel == null || playerModel == null)
            {
                tipPanel = Module.LoadPanel<TipPanel>();
                tipPanel.ShowTips("游戏存档为空",
                 "是否要创建新存档？",
                 () =>
                {
                    TimeModel timeModel0 = gameSaveController.GetTimeModel(0);
                    PlayerModel playerModel0 = gameSaveController.GetPlayerModel(0);
                    gameSaveController.DeleteGameSave(index);
                    
                    gameSaveController.CreateGameSave(index);
                    if(timeModel0 !=  null||playerModel0 != null)
                    {
                    gameSaveController.DeleteGameSave(0);
                    }
                    gameSaveController.CreateGameSave(0);
                    gameController.SetArchiveIndex(index);
                    gameController.SetGameState(GameState.Game_Main);
                    tipPanel.Close();
                    if (startPanel != null)
                    {
                        startPanel.UpdateStartPanel();
                    }
                    UpdateSaveGame();
                    Close();
                },
               () => { tipPanel.Close(); },
                true,
                "创建进入",
                "放弃创建");
            }
        }
        else //state!= GameState.Start
        {
            tipPanel = Module.LoadPanel<TipPanel>();
            tipPanel.ShowTips("覆盖游戏存档",
             "确定要覆盖该游戏存档吗？",
             () =>
            {
                gameSaveController.CoverGameSave(index, 0);
                gameController.SetArchiveIndex(index);
                tipPanel.Close();
                UpdateSaveGame();
            },
            () => { tipPanel.Close(); },
            true,
            "覆盖存档",
            "放弃覆盖");
        }
    }
    public void OnBtnDeleteGameClick(int index)
    {
        tipPanel = Module.LoadPanel<TipPanel>();
        tipPanel.ShowTips("删除游戏存档",
         "确定要删除该游戏存档吗？",
         () =>
        {
            gameSaveController.DeleteGameSave(index);
            tipPanel.Close();
            UpdateSaveGame();
            if (startPanel != null)
            {
                startPanel.UpdateStartPanel();
            }
        },
        () => { tipPanel.Close(); },
        true,
        "狠心删除",
        "再想想");
    }
    public void OnBtnCoveredGameClick(int index)
    {
        tipPanel = Module.LoadPanel<TipPanel>();
        tipPanel.ShowTips("覆盖游戏存档",
         "确定要覆盖该游戏存档吗？",
         () =>
        {
            gameSaveController.CoverGameSave(index,0);
            gameController.SetArchiveIndex(index);
            tipPanel.Close();
            UpdateSaveGame();
        },
        () => { tipPanel.Close(); },
        true,
        "覆盖存档",
        "放弃覆盖");

    }
    public void OnBtnLoadingGameClick(int index)
    {
        tipPanel = Module.LoadPanel<TipPanel>();
        tipPanel.ShowTips("加载游戏存档",
         "确定要加载该游戏存档吗？",
         () =>
        {
            // gameSaveController.(index);
            tipPanel.Close();
            UpdateSaveGame();
            TimeModel timeModel = gameSaveController.GetTimeModel(index);
            PlayerModel playerModel = gameSaveController.GetPlayerModel(index);
            gameSaveController.CoverGameSave(0,index);
            gameController.SetArchiveIndex(index);
            gameController.SetGameState(GameState.Game_Main);
        },
        () => { tipPanel.Close(); },
        true,
        "加载存档",
        "放弃加载");
    }
    #endregion
    #region 重写方法
    public override void OnLoaded(params object[] paream)
    {
        base.OnLoaded(paream);
        UpdateSaveGame();
        if (paream.Count() == 1)
        {
            startPanel = paream[0] as StartPanel;
        }
    }
    protected override void Awake()
    {
        base.Awake();
        RegisterSaveButtons();
    }
    #endregion
}
